<?php

class Visits_IndexController extends Core_Controller_Action_Standard
{
    
  public function init()
  {
    $this->_helper->requireUser();
    if( !$this->_helper->requireAuth()->setAuthParams('visits', null, 'view')->isValid() ) return;
  }
    
  public function indexAction()
  {
    // Get settings
    $settings = Engine_Api::_()->getApi('settings', 'core');
      
    // Get page
    $page = $this->_getParam('page');
    $mode = $this->_getParam('mode');
    
    // Get viewer
    $viewer = Engine_Api::_()->user()->getViewer();
    
   // Get visits of viewer
    $this->view->visits = $visits = Engine_Api::_()->getDbtable('visits', 'visits')->getVisits($viewer);
    
    // Get visits of viewer
    $visits->setItemCountPerPage($this->_getParam('itemCountPerPage', $settings->getSetting('visits.page', 9)));
    $visits->setCurrentPageNumber($this->_getParam('page'));
         
    // Render
    $this->_helper->content->setEnabled();

  }
  
  public function whoiviewedAction()
  {

    // Get settings
    $settings = Engine_Api::_()->getApi('settings', 'core');
      
    // Get page
    $page = $this->_getParam('page');
    
    // Get viewer
    $viewer = Engine_Api::_()->user()->getViewer();

    // Get visits by viewer
    $this->view->visits = $visits = Engine_Api::_()->getDbtable('visits', 'visits')->getVisitsByMe($viewer);
    $visits->setItemCountPerPage($this->_getParam('itemCountPerPage', $settings->getSetting('visits.page', 9)));
    $visits->setCurrentPageNumber($this->_getParam('page'));

    // Render
    $this->_helper->content->setEnabled();

  }

  public function moreAction()
  {
    // Get settings
    $settings = Engine_Api::_()->getApi('settings', 'core');
      
    // Get page
    $page = $this->_getParam('page');
    
    // Get viewer
    $viewer = Engine_Api::_()->user()->getViewer();

    // Get visits of viewer
    $this->view->visits = $visits = Engine_Api::_()->getDbtable('visits', 'visits')->getVisits($viewer);
    $visits->setItemCountPerPage($this->_getParam('itemCountPerPage', $settings->getSetting('visits.page', 9)));
    $visits->setCurrentPageNumber($this->_getParam('page'));

    // If no results
    if( $visits->getCurrentItemCount() <= 0 || $page > $visits->getCurrentPageNumber() ) {
      $this->_helper->viewRenderer->setNoRender(true);
      return;
    }

    // Force rendering now
    $this->_helper->viewRenderer->postDispatch();
    $this->_helper->viewRenderer->setNoRender(true);

  }
  
 public function whoiviewedMoreAction()
  {
    // Get settings
    $settings = Engine_Api::_()->getApi('settings', 'core');
      
    // Get page
    $page = $this->_getParam('page');
    
    // Get viewer
    $viewer = Engine_Api::_()->user()->getViewer();

    // Get visits of viewer
    $this->view->visits = $visits = Engine_Api::_()->getDbtable('visits', 'visits')->getVisitsByMe($viewer);
    $visits->setItemCountPerPage($this->_getParam('itemCountPerPage', $settings->getSetting('visits.page', 9)));
    $visits->setCurrentPageNumber($this->_getParam('page'));

    // If no results
    if( $visits->getCurrentItemCount() <= 0 || $page > $visits->getCurrentPageNumber() ) {
      $this->_helper->viewRenderer->setNoRender(true);
      return;
    }

    // Force rendering now
    $this->_helper->viewRenderer->postDispatch();
    $this->_helper->viewRenderer->setNoRender(true);

  }
}
